<template>
  <div class="todo-app">
    <h1>自律一下</h1>
    <div class="input-group">
      <input
        type="text"
        v-model="newTodo"
        placeholder="添加新的待办事项"
        @keyup.enter="addTodo"
      />
      <button @click="addTodo">添加</button>
    </div>
    <ul class="todo-list">
      <li v-for="(todo, index) in todos" :key="index" class="todo-item">
        <input
          type="checkbox"
          v-model="todo.completed"
          @change="toggleTodo(index)"
        />
        <span :class="{ completed: todo.completed }">{{ todo.text }}</span>
        <button class="delete-btn" @click="deleteTodo(index)">删除</button>
      </li>
    </ul>
  </div>
</template>

<script>
export default {
  data() {
    return {
      newTodo: '',
      todos: [
        { text: '复习计算机知识', completed: true },
        { text: '游泳60分钟', completed: false },
        { text: '跑步20分钟', completed: false }
      ]
    };
  },
  methods: {
    addTodo() {
      if (this.newTodo.trim()) {
        this.todos.push({
          text: this.newTodo.trim(),
          completed: false
        });
        this.newTodo = '';
      }
    },
    deleteTodo(index) {
      this.todos.splice(index, 1);
    },
    toggleTodo(index) {
      this.todos[index].completed = !this.todos[index].completed;
    }
  }
};
</script>

<style scoped>
.todo-app {
  max-width: 500px;
  margin: 0 auto;
  padding: 20px;
}

h1 {
  text-align: center;
  color: #333;
}

.input-group {
  display: flex;
  margin-bottom: 20px;
}

input[type="text"] {
  flex: 1;
  padding: 10px;
  border: 1px solid #ddd;
  border-radius: 4px 0 0 4px;
}

button {
  padding: 10px 15px;
  background-color: #4CAF50;
  color: white;
  border: none;
  border-radius: 0 4px 4px 0;
  cursor: pointer;
}

button:hover {
  background-color: #45a049;
}

.todo-list {
  list-style-type: none;
  padding: 0;
}

.todo-item {
  display: flex;
  align-items: center;
  padding: 10px;
  border-bottom: 1px solid #eee;
}

.todo-item input[type="checkbox"] {
  margin-right: 10px;
}

.completed {
  text-decoration: line-through;
  color: #888;
}

.delete-btn {
  margin-left: auto;
  background-color: #f44336;
  border-radius: 4px;
  padding: 5px 10px;
}

.delete-btn:hover {
  background-color: #d32f2f;
}
</style>